import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Date;

public class FileOutput {

	private static PrintWriter Std;
	private static PrintWriter Err;
	private static int ReturnCode;
	
	public static int getReturnCode() 
		{	return ReturnCode;	}

	public static void setReturnCode(int returnCode) 
		{	ReturnCode = returnCode;	}

	public static PrintWriter getStd() 
		{	return FileOutput.Std;	}

	public static void setStd(PrintWriter st) 
		{	FileOutput.Std = st;	}

	public static PrintWriter getErr() 
		{	return FileOutput.Err;		}

	public void setErr(PrintWriter err) 
		{	FileOutput.Err = err;	}

	public FileOutput(String StandardOutput, String ErrorOutput)
	{
		try 
		{	setStd( new PrintWriter(new FileWriter(StandardOutput)));
			setErr( new PrintWriter(new FileWriter(ErrorOutput)));		}
		
		catch (IOException e)
		{	System.err.println(e);	}			
	}

	public static void AppendToStandardOutput( String str ) 
		{	FileOutput.Std.println( str );
			FileOutput.Std.flush();
		}

	public static void AppendToErrorOutput( String str ) 
		{	FileOutput.Err.println( str );	
			FileOutput.Err.flush();
		}

	public static void printCommandHeader(String[] command, int rc, String date)
	{
	    	String commandString = "";
	    	
	    	for ( String cmdArgs : command  )
	    	{    commandString = commandString + " " + cmdArgs + " ";   	}
	    	    
		if ( rc == 0 )
		{
			FileOutput.AppendToStandardOutput( "=========================================================" );
			FileOutput.AppendToStandardOutput( "\t\t\t" + date );
			FileOutput.AppendToStandardOutput( "Running this command: " + commandString );
			FileOutput.AppendToStandardOutput( "Return value of this command was: " + rc );
			FileOutput.AppendToStandardOutput( "Follows command output: " );
			FileOutput.AppendToStandardOutput( "=========================================================" );
		}
		else
		{
			FileOutput.AppendToErrorOutput( "=========================================================" );
			FileOutput.AppendToErrorOutput( "\t\t\t" + date );
			FileOutput.AppendToErrorOutput( "Running this command: " + command );
			FileOutput.AppendToErrorOutput( "Return value of this command was: " + rc );
			FileOutput.AppendToErrorOutput( "Follows command output: " );
			FileOutput.AppendToErrorOutput( "=========================================================" );
		}
	}
	
	    public static String RunCommand( boolean isPrintable, String command[] )
	    {
		String WholeString = "";
		String Date = "";
		boolean isFirstLine;
		ProcessBuilder builder;	
		Process process;
		int returnCode;
		
		if ( isPrintable == true )
		{	Date date = new Date();
			Date = date.toString();		}
		try
		{
		    
			builder = new ProcessBuilder( command );
			builder.redirectErrorStream(true);
			process = builder.start();

		    	BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream()));

			setReturnCode( process.waitFor() );
			returnCode = getReturnCode();
			
			String line;
			isFirstLine = true;
					
			while ((line = bri.readLine()) != null) 
			{	
			    if ( isPrintable )
			    {	
				if ( isFirstLine )
				{	
				    FileOutput.printCommandHeader( command, returnCode, Date );
				    isFirstLine = false;	
				}
				
				FileOutput.AppendToStandardOutput( line );	
			    }	
			
			    WholeString = WholeString + line + "\n" ;							
			}
			
			WholeString = WholeString.replaceAll("\n$", "");
				
		} 
		catch ( Exception e )
			{	e.printStackTrace();	}

		return WholeString;
		}

}
